package com.next.aappublicapp.views;

import java.util.ArrayList;
import java.util.List;

import android.annotation.SuppressLint;
import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TableRow;
import android.widget.TextView;

import com.next.aap.client.BaseItem;
import com.next.aappublicapp.R;
import com.next.aappublicapp.handlers.ItemClickHandler;
import com.next.aappublicapp.handlers.ItemListClickHandler;
import com.next.aappublicapp.setting.ClientSideSetting;

@SuppressLint("NewApi")
public class ImageTextScrollerView<T extends BaseItem> extends LinearLayout implements ItemClickHandler<T>{

	private int categoryText;
	private ItemListClickHandler<T> itemClickedHandler;
	private List<ImageTextView<T>> imageTextViews;
	private List<T> itemList;
	private TextView noContentTextView;
	private TextView categoryHeading;
	
	public ImageTextScrollerView(Context context, AttributeSet attrs, int defStyle, int categoryText,ItemListClickHandler<T> itemClickedHandler) {
		super(context, attrs, defStyle);
		this.categoryText = categoryText;
		createScrollerView(context, R.layout.item_scroller_view, categoryText,itemClickedHandler);
	}
	public ImageTextScrollerView(Context context, AttributeSet attrs, int categoryText,ItemListClickHandler<T> itemClickedHandler) {
		super(context, attrs);
		this.categoryText = categoryText;
		createScrollerView(context, R.layout.item_scroller_view, categoryText,itemClickedHandler);
	}
	public ImageTextScrollerView(Context context, int categoryText,ItemListClickHandler<T> itemClickedHandler) {
		super(context);
		this.categoryText = categoryText;
		createScrollerView(context, R.layout.item_scroller_view, categoryText,itemClickedHandler);
	}
	private void createScrollerView(Context context,int viewId, int categoryText,ItemListClickHandler<T> itemClickedHandler){
		this.itemClickedHandler = itemClickedHandler;
		LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View view = inflater.inflate(viewId, this);
        TableRow tableRow = (TableRow)view.findViewById(R.id.tableRow);
        categoryHeading = (TextView)view.findViewById(R.id.categoryHeading);
        noContentTextView = (TextView)view.findViewById(R.id.noItemText);
        
        imageTextViews = new ArrayList<ImageTextView<T>>();
    	for(int i=0;i<ClientSideSetting.MAX_NEWS_ITEMS;i++){
    		ImageTextView<T> imageTextView = new ImageTextView<T>(context,i,this);
    		tableRow.addView(imageTextView);
    		imageTextViews.add(imageTextView);
        }
    	categoryHeading.setText(categoryText);
    	noContentTextView.setText("No "+context.getString(categoryText) +" available");
	}

	public void setItems(List<T> itemList){
		this.itemList = itemList;
		int i=0;
    	for(T oneNewsItem:itemList){
    		ImageTextView<T> imageTextView = imageTextViews.get(i);
    		imageTextView.setItem(oneNewsItem);
    		imageTextView.setVisibility(VISIBLE);
    		i++;
        }
    	for(int j=i;j<ClientSideSetting.MAX_NEWS_ITEMS;j++){
    		ImageTextView<T> imageTextView = imageTextViews.get(j);
    		imageTextView.setVisibility(GONE);
    	}
    	if(itemList.isEmpty()){
    		noContentTextView.setVisibility(VISIBLE);
    	}else{
    		noContentTextView.setVisibility(GONE);
    	}
	}
	
	@Override
	public void itemClicked(int position, T item) {
		if(itemList instanceof ArrayList){
			itemClickedHandler.ItemClicked(position, item, itemList);	
		}else{
			itemClickedHandler.ItemClicked(position, item, new ArrayList(itemList));
		}
		
	}

}
